// Modules to control application life and create native browser window


const { app, BrowserWindow, Menu, globalShortcut, ipcMain, Tray, BrowserView} = require('electron')
const path = require('path')
const {menu} = require("./config/menu_config");
const {add} = require("./controller");
const {listen} = require("./config/batchListen")
//这里的配置手动写的，也可以使用cross-env插件配置
const mode = process.env.API_ENV === "dev"
process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true" //解决跨域会有警告,用这个之后就不显示警告了
/*隐藏electron创听的菜单栏*/
Menu.setApplicationMenu(null)

// try {
//     require('electron-reloader')(module,{});
// } catch (_) {}
let mainWindow;
function createWindow() {
    // Create the browser window.
    mainWindow = new BrowserWindow({
        width: 800,
        height: 600,
        frame: true /*是否展示顶部导航  去掉关闭按钮  最大化最小化按钮*/ ,
        webPreferences: {
            preload: path.join(__dirname, 'preload.js'),
            // nodeIntegration:true,
            // contextIsolation:true,  能解决渲染进程可以访问nodejs 但是有一定的风险
            webSecurity: false //解决跨域
        },
    })

    // and load the index.html of the app.
    // mainWindow.loadFile('index.html')  修改成如下

    mainWindow.loadURL(mode ? 'http://localhost:5173' : `file://${path.join(__dirname, '../dist', '/index.html')}`)

    // Open the DevTools.
    // mainWindow.webContents.openDevTools()
    Menu.setApplicationMenu(menu)
}

app.whenReady().then(() => {
    createWindow()

    app.on('activate', function() {
        // On macOS it's common to re-create a window in the app when the
        // dock icon is clicked and there are no other windows open.
        if (BrowserWindow.getAllWindows().length === 0) createWindow()
    })
    createTray()
    listen()
})


add(app,globalShortcut)

// 监听'show-context-menu'消息，并在窗口中显示右键菜单
ipcMain.on('show-context-menu', (event, position) => {
    position = JSON.parse(position)
    const menu = Menu.buildFromTemplate([
        {
            label: 'Menu Item 1',
            click: () => {
                console.log('Menu Item 1 clicked')
            }
        },
        {
            label: 'Menu Item 2',
            click: () => {
                console.log('Menu Item 2 clicked')
            }
        }
    ])
    menu.popup({
        window: BrowserWindow.fromWebContents(event.sender),
        x: position.x,
        y: position.y
    })
})
function createTray(){
    // 创建托盘
    const contextMenu = Menu.buildFromTemplate([
        {
            label: '显示',
            click: () => {
                for(let i in this.group) {
                    if(this.group[i]) {
                        if(mainWindow.isMinimized()) mainWindow.restore()
                        mainWindow.show()
                    }
                }
            }
        }, {
            label: '退出',
            click: () => {
                app.quit()
            }
        }
    ])
    console.log(__dirname)
    const trayIco = path.join(__dirname, '../src/assets/img/img.png')
    console.log(trayIco)
    let tray = new Tray(trayIco)
    tray.setContextMenu(contextMenu)
    tray.setToolTip(app.name)
}

ipcMain.on("window-new", (event, position) => createWindows(position));
app.commandLine.appendSwitch('ignore-certificate-errors', 'true');

function createWindows(options) {
    console.log("------------开始创建窗口...");
    const win = new BrowserWindow({ width: 800, height: 600 })
    win.loadURL('http://localhost:5173/card/DesktopCard#/')
}
